package com.cykj.v_m_sprintboot.controller;

import com.cykj.v_m_sprintboot.bean.TbLoss;
import com.cykj.v_m_sprintboot.bean.TbOut;
import com.cykj.v_m_sprintboot.service.TbLossService;
import com.cykj.v_m_sprintboot.tool.GetTime;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TbLossController {
    @Autowired
    private TbLossService tbLossService;
    //获取所有集合
    @RequestMapping("lossList")
    @ResponseBody
    public Map<String,Object> LossList(@RequestBody TbLoss tbLoss){
        tbLoss.setStateListId(1);
        RowBounds row =new RowBounds((tbLoss.getPage()-1)*tbLoss.getSize(),tbLoss.getSize());
        Map<String,Object> map=new HashMap<>();
        List<TbLoss> lossList=tbLossService.LossList(tbLoss,row);
        map.put("count",tbLossService.countLossList(tbLoss));
        map.put("lossList",lossList);
        return map;
    }
    //获取所的商品分类
    @RequestMapping("getLossGoodsType")
    @ResponseBody
    public Map<String,Object> getLossGoodsType(){
        Map<String,Object> map=new HashMap<>();
        List<TbLoss> goodsType=tbLossService.getGoodsType();
        map.put("goodsType",goodsType);
        return map;
    }
    //获取商品名称
    @RequestMapping("getLossGoodsName")
    @ResponseBody
    public Map<String,Object> getLossGoodsName(@RequestBody TbLoss tbLoss ){
        String[] strings=new String[2];
        if (tbLoss.getGoodsType().equals("饮料")||tbLoss.getGoodsType().equals("酒类")){
            strings[0]="件";
            strings[1]="瓶";
        }else {
            strings[0]="件";
            strings[1]="包";
        }
        Map<String,Object> map=new HashMap<>();
        List<TbLoss> goodsName=tbLossService.getGoodsName(tbLoss);
        map.put("goodsName",goodsName);
        map.put("goodsUnit",strings);
        return map;
    }


    //添加损耗的数据
    @RequestMapping("addLossInfo")
    @ResponseBody
    public String addLossInfo(@RequestBody TbLoss tbLoss){
        tbLoss.setLossTime(GetTime.getTime());
        //单位的换算
        double num=0;
        if (tbLoss.getGoodsUnit().equals("瓶") ||tbLoss.getGoodsUnit().equals("包")){
            num=tbLoss.getLossNum()/10;
        }else{
            num=tbLoss.getLossNum();
        }
        tbLossService.addLossInfo(tbLoss);
        if (num<1) {
            return "申请成功";
        }
        return "损耗数量过大，请先联系上级领导说明情况";
    }
    //修改数据内容
    @RequestMapping("updateLossList")
    @ResponseBody
    public String updateLossList(@RequestBody TbLoss tbLoss){
        tbLoss.setLossTime(GetTime.getTime());
        tbLossService.updateLossList(tbLoss);
        double num=0;
        if (tbLoss.getGoodsUnit().equals("瓶") ||tbLoss.getGoodsUnit().equals("包")){
            num=tbLoss.getLossNum()/10;
        }else{
            num=tbLoss.getLossNum();
        }
        tbLossService.addLossInfo(tbLoss);
        if (num<1) {
            return "修改成功";
        }
        return "已修改,损耗数量过大，请联系上级领导说明情况!!";
    }

    //数据删除
    @RequestMapping("delLossInfo")
    @ResponseBody
    public String delLossInfo(@RequestBody TbLoss tbLoss){
        System.out.println(tbLoss.getId()+"************************************************");
        tbLoss.setStateListId(3);
       tbLossService.delLossInfo(tbLoss);
       return "移除成功";

    }





}
